package com.example.statistics.ui.sales

import android.os.Bundle
import android.view.View
import com.example.statistics.R
import com.example.statistics.bean.ProductBean
import com.example.statistics.common.BaseLoadingListActivity
import com.example.statistics.ex.httpPost
import com.example.statistics.ex.imageDisplay
import com.example.statistics.net.UrlConfig
import com.example.statistics.ui.product.ProductInfoEditActivity
import com.example.statistics.ui.product.ProductInfoListActivity
import com.example.statistics.utils.Constants
import com.example.statistics.utils.activityForResult
import com.jhj.slimadapter.SlimAdapter
import com.jhj.slimadapter.holder.ViewInjector
import com.example.statistics.ex.glideRound

class CommonProductListActivity : BaseLoadingListActivity<ProductBean>() {


    override val title: String
        get() = "商品列表"

    override val url: String
        get() = UrlConfig.PRODUCT_INFO_LIST

    override val itemLayoutRes: Int
        get() = R.layout.list_item_product_info

    override val inputSearch: Boolean
        get() = true

    override val inputSearchFunc: (ProductBean, String) -> Boolean = { bean, str ->
        bean.productNo.contains(str) || (bean.factoryNo + bean.productName).replace(".", "").contains(str)
    }
    override val httpParams: MutableList<Pair<String, String>>
        get() = mutableListOf("isCommon" to "1")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        topBarRightImage {
            activityForResult<ProductInfoListActivity>(Constants.TYPE to "猪副", Constants.ACTION to Constants.SELECTOR) {
                val data = intent.getSerializableExtra(Constants.DATA) as ProductBean?
                        ?: return@activityForResult
                httpPost<Any>(UrlConfig.PRODUCT_COMMON_MODIFY, config = {
                    "id" to data.id
                    "isCommon" to 1
                }) {
                    refresh()
                }
            }
        }
    }

    override fun itemViewConvert(adapter: SlimAdapter, injector: ViewInjector, bean: ProductBean, position: Int) {
        val imageList = bean.imageList?.map { it.path }
        injector.text(R.id.tv_title, bean.factoryNo + bean.productName)
                .text(R.id.tv_status, bean.productNo)
                .visibility(R.id.tv_status, if (bean.productNo.isEmpty()) View.GONE else View.VISIBLE)
                .glideRound(R.id.iv_product, if (imageList.isNullOrEmpty()) "" else imageList[0])
                .clicked {
                    activityForResult<ProductInfoEditActivity>(Constants.ID to bean.id, Constants.TYPE to "猪副") {
                        refresh()
                    }
                }
                .clicked(R.id.iv_product) {
                    imageDisplay(imageList.orEmpty())
                }
        /*  .longClicked {
              messageDialog(msg = "是否删除“${bean.factoryNo + bean.productName}”?") { alertFragment, view ->
                  HttpCall.get(UrlConfig.PRODUCT_INFO_DELETE)
                          .addParam("id", bean.id)
                          .enqueue(object : DataDialogHttpCallback<Int>(this) {
                              override fun onSuccess(data: Int?, resultType: ResultType) {
                                  refresh()
                              }
                          })
              }
              false
          }*/
    }
}